package me.stephenj.contactsweb.service;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Service
public class UploadService {

    private static String rootPath = "/home/stephen/www/images";

    public String uploadImage(MultipartFile file) throws Exception{
        String filePrefixname = RandomStringUtils.randomAlphanumeric(10);
        // File fileDir = new File(rootPath + File.separator);
        String filename = file.getOriginalFilename();
        String[] filenameArray = filename.split("\\.");
        String fileExtendName = filenameArray[filenameArray.length - 1];
        String newFilename = filePrefixname + "." + fileExtendName;
        file.transferTo(new File(rootPath + File.separator + newFilename));
        return newFilename;
    }
}
